﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web;

public partial class Controls_MakeRepayment : System.Web.UI.UserControl
{
    int mLoanID = 0;
    int mPayID = 0;
    int mCurrencyID = 0;
    decimal mCurrencyChangeRate = 0;
    decimal mCurrenty = 0;

    bool mIsNew = true;

    public bool IsNew
    {
        set { mIsNew = value; }
    }

    public int LoanID
    {
        set { mLoanID = value; }
    }

    public int PayID
    {
        set { mPayID = value; }
    }

    public int CurrencyID
    {
        set { mCurrencyID = value; }
    }

    public decimal CurrencyChangeRate
    {
        set { mCurrencyChangeRate = value; }
    }

    public decimal Currenty
    {
        set { mCurrenty = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        transactionDateCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;

        if (!IsPostBack)
        {
                    
        }
    }
    
    protected void ClearPersonalDataForms()
    {
        infoBoxTitleLabel.Text = string.Empty;
        transactionDateTextBox.Text = string.Empty;
        transactionAmounTextBox.Text = string.Empty;
    }

    public void Show()
    {
        string appPath = Utils.GetApplicationPath(Request);
        ClearPersonalDataForms();
               
        this.programmaticModalPopup.Show();
    }
    
    protected void saveButton_Click(object sender, EventArgs e)
    {
        TextBox dateTextBox = (TextBox)this.FindControl("transactionDateTextBox");
        DateTime payDate = Convert.ToDateTime(dateTextBox.Text);

        int iteration = Utils.ModuleCredits().GetIterationByLoanID(mLoanID);

        DateTime dateGrafic = Utils.ModuleCredits().GetDateGraficByLoanID(mLoanID, iteration);

        decimal delayDaysCount = (decimal)((payDate - dateGrafic).TotalDays);
                
        decimal profitGrafic = Utils.ModuleCredits().GetProfitByLoanID(mLoanID, iteration);
        decimal profitCumulativ = profitGrafic;

        decimal cursValue = 1;
        decimal mainGrafic = Utils.ModuleCredits().GetMainBYLoanID(mLoanID, iteration)*cursValue;
        decimal mainCumulativ = mainGrafic;

        Credits.DataObjects.LoanObject loanInfo = Utils.ModuleCredits().GetLoanObjectByID(mLoanID);
        decimal penaltyRate = loanInfo.ContractPenalty;

        decimal penalitateCurent = (((profitGrafic + mainGrafic) * penaltyRate) / 100)*delayDaysCount;
        if (penalitateCurent < 0)
        {
            penalitateCurent = 0;
        }

        decimal profitDiff = profitGrafic - profitCumulativ;
        if (profitDiff < 0)
        {
            profitDiff = 0;
        }

        decimal inputCurent = 0;
        TextBox inputTextBox = (TextBox)this.FindControl("transactionAmounTextBox");
        decimal.TryParse(inputTextBox.Text.Replace('.', ','), out inputCurent);

        decimal profitCurent = 0;
        decimal avansCurent = 0;
        decimal mainCurrent = 0;
        if (profitDiff <= inputCurent)
        {
            profitCurent = profitDiff;
            mainCurrent = inputCurent - penalitateCurent - profitCurent;
            if (mainCurrent > (mainGrafic - mainCumulativ))
            {
                mainCurrent = mainGrafic - mainCumulativ;
            }
            avansCurent = inputCurent - penalitateCurent - profitCurent - mainCurrent;
        }
        else
        {
            profitCurent = inputCurent;
        }

        decimal main = mainCurrent;

        decimal profit = profitCurent;

        decimal penalty = penalitateCurent;

        decimal currenty = cursValue;

        decimal commission = 0;

        int loanValue = mLoanID;        

        int payID = 0;
        // 0 - new input;
        // 1 - edit/delete;

        decimal avansValue = avansCurent;

        int newPayID = 0;
        decimal lateValuta = 0;
        if (payID == 0)
        {
            bool isPenalty = false;
            if(penalty!=0)
            {
                isPenalty = true;
            }

            //bool result = Utils.ModuleCredits().AddNewRefund(loanValue, iteration, payDate
            //    , main, profit, penalty, currenty, commission, avansValue, 0, 0, isPenalty, lateValuta, out newPayID);
        }
        else
        {
            if (inputCurent == 0)
            {
                //delecte
                bool resultDelete = Utils.ModuleCredits().DeleteRefundByPayID(payID);
            }
            else
            {
                //update
                //bool resultUpdate = Utils.ModuleCredits().UpdateRefundByPayID(payID, payDate, main, profit, penalty, currenty, commission, avansValue);
                newPayID = payID;
            }
        }

        bool resultFinishedTranche = Utils.ModuleCredits().UpdateFinishedTranche(loanValue, iteration);
        
        if (avansValue > 0)
        {
            //bool resultAvans = Utils.ModuleCredits().AddAvansRefund(loanValue, iteration, payDate, avansValue, newPayID);
        }

        RefundsWindowEventsArg args = new RefundsWindowEventsArg(true);
        EntrySelected(this, args);
        
    }

    protected void cancelButton_Click(object sender, EventArgs e)
    {
        RefundsWindowEventsArg args = new RefundsWindowEventsArg(true);
        EntrySelected(this, args);
    }

    public class RefundsWindowEventsArg : EventArgs
    {
        private bool resultRefund;
        public bool ResultRefund
        {
            get { return resultRefund; }
        }

        public RefundsWindowEventsArg(bool result)
        {
            resultRefund = result;
        }
    }

    public event CustomItemEventHandler EntrySelected;
    public delegate void CustomItemEventHandler(object sender, Controls_MakeRepayment.RefundsWindowEventsArg e);
}